package com.dubito.github.ext

//Boolean扩展 再见了if else

//巧用协变与密封类(增强版枚举)
sealed class BooleanExt<out T>

//data是协变点(get方法)
class Success<T>(val data: T) : BooleanExt<T>()

//Nothing是任何类的子类
object OtherWise : BooleanExt<Nothing>()

//inline提升性能
inline fun <T> Boolean.yes(block: () -> T): BooleanExt<T> =
    when {
        this -> Success(block())
        else -> OtherWise
    }

inline fun <T> Boolean.no(block: () -> T): BooleanExt<T> = when {
    this -> OtherWise
    else -> Success(block())
}

inline fun <T> BooleanExt<T>.otherwise(block: () -> T): T =
    when (this) {
        is Success -> this.data
        OtherWise -> block()
    }

